package io.github.wslxm.springbootplus2.manage.test.controller;

import io.github.wslxm.springbootplus2.core.base.annotation.LimitScope;
import io.github.wslxm.springbootplus2.core.base.annotation.XjCurrentLimit;
import io.github.wslxm.springbootplus2.core.base.annotation.XjCurrentLimitAll;
import io.github.wslxm.springbootplus2.core.base.controller.BaseController;
import io.github.wslxm.springbootplus2.core.constant.BaseConstant;
import io.github.wslxm.springbootplus2.core.result.Result;
import io.github.wslxm.springbootplus2.manage.test.service.GcTestService;
import io.github.wslxm.springbootplus2.utils.XjLimitUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.temporal.ChronoUnit;

/**
 * 限流测试
 *
 * @author ws
 * @email 1720696548@qq.com
 * @date 2025-03-14 15:43:20
 */
@RestController
@RequestMapping(BaseConstant.Uri.API_ADMIN + "/test/limit")
@Api(tags = "base--test--限流测试")
public class LimitTestController extends BaseController<GcTestService> {

    @GetMapping(value = "/{id}")
    @ApiOperation(value = "测试1")
    @XjCurrentLimitAll({
            @XjCurrentLimit(qps = 1000, scope = LimitScope.GLOBAL, timeUnit = ChronoUnit.MINUTES, errorMsg = "当前接口每分钟并发数为1000, 请求人数过多,请稍后重试!"),
            @XjCurrentLimit(qps = 100, scope = LimitScope.IP, timeUnit = ChronoUnit.HOURS, errorMsg = "当前接口同IP一小时只能请求100次"),
            @XjCurrentLimit(qps = 10, scope = LimitScope.IP, timeUnit = ChronoUnit.MINUTES, errorMsg = "当前接口同IP一分钟只能请求10次"),
            @XjCurrentLimit(qps = 1, scope = LimitScope.IP, timeUnit = ChronoUnit.SECONDS, errorMsg = "当前接口同IP一秒钟只能请求2次"),
    })
    public Result<Void> test1(@PathVariable String id) {
        return Result.success();
    }

    @GetMapping(value = "/xx/{id}")
    @ApiOperation(value = "2")
    @XjCurrentLimit(qps = 5, scope = LimitScope.CUSTOM, limitKey = "#id", timeUnit = ChronoUnit.MINUTES, errorMsg = "当前接口同ID一分钟只能请求5次")
    public Result<Void> test2(@PathVariable String id) {
        return Result.success();
    }

    @GetMapping(value = "/xx2/{id}")
    @ApiOperation(value = "3")
    public Result<Void> test3(@PathVariable String id) {
        XjLimitUtil.limit("test-"+ id,5, ChronoUnit.MINUTES, "当前xx2接口同ID一分钟只能请求5次");
        return Result.success();
    }
}
